#define RV 9
#define LV 6
#define RVB 6 
#define LVB 11
#define RV 9
#define LV 10
int p=0;
int trigPin = 7 ; // назначаем имя для Pin8
int echoPin = 8; // назначаем имя для Pin9
void setup() {
  Serial.begin(115200);
  pinMode(trigPin, OUTPUT); // назначаем trigPin, как выход
  pinMode(echoPin, INPUT); // назначаем echoPin, как вход
} 
void runStraight () {
  analogWrite(RV, 155); 
  analogWrite(LV, 155);
  analogWrite(LVB, 0);
  analogWrite(RVB, 0); 
}
void turnLeft () {
  analogWrite(RV, 55); 
  analogWrite(LV, 155);
  analogWrite(LVB, 0); 
  analogWrite(RVB, 0); 
}
void turRight () {
  analogWrite(RV, 155); 
  analogWrite(LV, 55);
  analogWrite(LVB, 0);
  analogWrite(RVB, 0); 
}
void turStop () {
  analogWrite(RV, 0); 
  analogWrite(LV, 0);
  analogWrite(LVB, 0);
  analogWrite(RVB, 0);  

}
void loop() {

digitalWrite(12, HIGH);
  int signalLeft = digitalRead(2); 
  int signalCentralLeft = digitalRead(3); 
  int signalCentralRight= digitalRead(4); 
  int signalRight = digitalRead(5); 
  Serial.print(signalLeft); 
  Serial.print(signalCentralLeft); 
  Serial.print(signalCentralRight); 
  Serial.println(signalRight);
  Serial.println(p);
  int duration, cm; // назначаем переменную "cm" и "duration" для показаний датчика
  digitalWrite(trigPin, LOW); // изначально датчик не посылает сигнал
  delayMicroseconds(2); // ставим задержку в 2 ммикросекунд
  digitalWrite(trigPin, HIGH); // посылаем сигнал
  delayMicroseconds(10); // ставим задержку в 10 микросекунд
  digitalWrite(trigPin, LOW); // выключаем сигнал
  duration = pulseIn(echoPin, HIGH); // включаем прием сигнала
  cm = duration / 58; // вычисляем расстояние в сантиметрах
  if (cm > 7) {      // условия если больше 7 см то....
  if (signalLeft == 0 and signalRight == 0 and signalCentralLeft == 1 and signalCentralRight == 1) { 
    runStraight(); 
  } 
  else if (signalRight == 0 and signalLeft == 1) { 
    turRight();
  } 
  else if (signalLeft == 0 and signalRight == 1){ 
    turnLeft();
  }
  else if (signalLeft == 1 and signalRight == 1 and signalCentralLeft == 1 and signalCentralRight == 1 and p==0){
    analogWrite(RV, 155); 
    analogWrite(LV, 0);
    analogWrite(LVB, 155);
    analogWrite(RVB, 0); 
  }
  else if  (p==0 and signalLeft == 0 and signalRight == 0 and signalCentralLeft == 0 and signalCentralRight == 0){
      analogWrite(RV, 0); 
      analogWrite(LV, 0);
      analogWrite(LVB, 0);
      analogWrite(RVB, 0); 
      delay(3000);
      analogWrite(RV, 155); 
      analogWrite(LV, 0);
      analogWrite(LVB, 155);
      analogWrite(RVB, 0); 
      p=p+1;
  }
   else if  (p==1 and signalLeft == 1 and signalRight == 1 and signalCentralLeft == 1 and signalCentralRight == 1){
      analogWrite(RV, 155); 
      analogWrite(LV, 155);
      analogWrite(LVB, 0);
      analogWrite(RVB, 0); 
      p=p+1;
  }
   else if  (p==2 and signalLeft == 0 and signalRight == 0 and signalCentralLeft == 0 and signalCentralRight == 0){
      analogWrite(RV, 0); 
      analogWrite(LV, 0);
      analogWrite(LVB, 0);
      analogWrite(RVB, 0); 
      delay(3000);
      analogWrite(RV, 155); 
      analogWrite(LV, 0);
      analogWrite(LVB, 155);
      analogWrite(RVB, 0); 
      p=p+1;
   }
    else if  (p==3 and signalLeft == 1 and signalRight == 1 and signalCentralLeft == 1 and signalCentralRight == 1){
      analogWrite(RV, 0); 
      analogWrite(LV, 155);
      analogWrite(LVB, 0);
      analogWrite(RVB, 155); 
      p=p+1;
    }
    else if (signalLeft == 1 and signalRight == 1 and signalCentralLeft == 1 and signalCentralRight == 1 and p==4){
    analogWrite(RV, 0); 
    analogWrite(LV, 155);
    analogWrite(LVB, 0);
    analogWrite(RVB, 155); 
  }
  else if  (p==4 and signalLeft == 0 and signalRight == 0 and signalCentralLeft == 0 and signalCentralRight == 0){
      analogWrite(RV, 0); 
      analogWrite(LV, 0);
      analogWrite(LVB, 0);
      analogWrite(RVB, 0); 
      delay(3000);
      analogWrite(RV, 155); 
      analogWrite(LV, 0);
      analogWrite(LVB, 155);
      analogWrite(RVB, 0); 
      p=p+1;
  }
   else if  (p==5 and signalLeft == 1 and signalRight == 1 and signalCentralLeft == 1 and signalCentralRight == 1){
      analogWrite(RV, 155); 
      analogWrite(LV, 155);
      analogWrite(LVB, 0);
      analogWrite(RVB, 0); 
      p=p+1;
  }
   else if  (p==6 and signalLeft == 0 and signalRight == 0 and signalCentralLeft == 0 and signalCentralRight == 0){
      analogWrite(RV, 0); 
      analogWrite(LV, 0);
      analogWrite(LVB, 0);
      analogWrite(RVB, 0); 
      delay(3000);
      analogWrite(RV, 155); 
      analogWrite(LV, 0);
      analogWrite(LVB, 155);
      analogWrite(RVB, 0); 
      p=p+1;
   }
    else if  (p==7 and signalLeft == 1 and signalRight == 1 and signalCentralLeft == 1 and signalCentralRight == 1){
      analogWrite(RV, 0); 
      analogWrite(LV, 155);
      analogWrite(LVB, 0);
      analogWrite(RVB, 155); 
      p=p+1;
        } else if (signalLeft == 1 and signalRight == 1 and signalCentralLeft == 1 and signalCentralRight == 1 and p==8){
    analogWrite(RV, 155); 
    analogWrite(LV, 0);
    analogWrite(LVB, 155);
    analogWrite(RVB, 0); 
  }    else if  (p==8 and signalLeft == 0 and signalRight == 0 and signalCentralLeft == 0 and signalCentralRight == 0){
      analogWrite(RV, 0); 
      analogWrite(LV, 0);
      analogWrite(LVB, 0);
      analogWrite(RVB, 0); 
      delay(3000);
      analogWrite(RV, 155); 
      analogWrite(LV, 0);
      analogWrite(LVB, 155);
      analogWrite(RVB, 0); 
      p=p+1;
   } else if  (p==9 and signalLeft == 1 and signalRight == 1 and signalCentralLeft == 1 and signalCentralRight == 1){
      analogWrite(RV, 255); 
      analogWrite(LV, 255);
      analogWrite(LVB, 0);
      analogWrite(RVB, 0); 
      p=p+1;
  } else if  (p==10 and signalLeft == 0 and signalRight == 0 and signalCentralLeft == 0 and signalCentralRight == 0){
      analogWrite(RV, 0); 
      analogWrite(LV, 0);
      analogWrite(LVB, 0);
      analogWrite(RVB, 0); 
      delay(3000);
      analogWrite(RV,0); 
      analogWrite(LV, 155);
      analogWrite(LVB, 0);
      analogWrite(RVB, 155); 
      p=p+1;
  } else if  (p==11 and signalLeft == 1 and signalRight == 1 and signalCentralLeft == 1 and signalCentralRight == 1){
      analogWrite(RV, 155); 
      analogWrite(LV, 0);
      analogWrite(LVB, 155);
      analogWrite(RVB, 0);
      p=p+1;
        }      
  }
  else{             // иначе тоесть ечли меньше 7 см то...
      analogWrite(RV, 0); 
      analogWrite(LV, 0);
      analogWrite(LVB, 0);
      analogWrite(RVB, 0);
  }

}